function initSlide(data){

   // alert(data['0']['img']);
   var paths=new Array();
   for(var i=0; i< data.length; i++){
        paths[i] = 'http://wap.jqmao.cn/statics/uploads/'+data[i]['img'];
   }
   var h;
 
   if(api.winWidth>320){
 
   		h=180;
   }else{
   		h=160;
   }
   
var UIScrollPicture = api.require('UIScrollPicture');
UIScrollPicture.open({
    rect: {
        x: 0,
        y: 0,
        w: api.winWidth,
        h: h
    },
    data: {
        paths: paths,
    },
    styles: {
        caption: {
            height: 35,
            color: '#E0FFFF',
            size: 13,
            bgColor: '#696969',
            position: 'bottom'
        },
        indicator: {
            align: 'right',
            color: '#FFFFFF',
            activeColor: '#FF0000'
        }
    },
    //placeholderImg: 'widget://res/slide1.jpg',
    contentMode: 'scaleToFill',
    interval: 8,
    loop: true,
    fixedOn: api.frameName,
    fixed: false
},function( ret, err ){
    if( ret ){
    //alert( JSON.stringify(ret));
		if(ret.eventType=='click'){
		//alert(ret.index)
			switch(ret.index){
				 case 0:getbannersid(1);	
				 break;
				 case 1:getbannersid(2);
				 break;
				 case 2:getbannersid(3);
				 break;
				 case 3:getbannersid(4);
				 break;
				 case 4:getbannersid(5);
				 break;	
			}
		}
         
    }else{
         //alert( JSON.stringify( err ) );
    }
});
      
}
function getbannersid(index){

	api.ajax({
	           	url:'http://wap.jqmao.cn/getBannerInfo?index='+index
	         },function(ret,err){
	         	//coding...
	         	
	         	//openGoodsDetail(ret.sid);
	         	if(ret.sid){
	         		openGoodsDetail(ret.sid)
	         	}else if(ret.name!=null){
	         	
	         		openHeader(''+ret.name)
	         	}else if(ret.link!=null){
	         		openUrl(''+ret.link,''+ret.h_name)
	         	}
	         });
}


apiready = function () {
		
		

//监听回到前台刷新页面
/*api.addEventListener({
    name:'resume'
}, function(ret, err){         
    _openFrame(0)
	        
});*/
if(api.connectionType=='none'){
  error();
  var reloading = $api.byId('reloading');
  $api.addEvt(reloading, 'click', function(){
  	_openFrame(0)
  });
  	return;
  }

api.setFrameAttr({
            name: api.frameName,
            customRefreshHeader:"UIPullRefreshFlash",
            bounces:true
        });
    var total='';




function getData(type,isReload,currentPage) {
    var pageSize = 10;
    currentPage=isReload?0:currentPage;

    api.ajax({
        url: 'http://wap.jqmao.cn/getGoodsList',
        method: 'post',
        data: {
            values: {name: type, pageSize: pageSize,currentPage:currentPage}
        }
    }, function (ret, err) {

        if (ret) {
            total = ret['total']
            var content = $api.byId('act-content');
            var tpl = $api.byId('act-template').text;
            var tempFn = doT.template(tpl);
            //alert($api.getStorage('type'));
            //alert(type)
           
            if (isReload) {
                 $api.setStorage("currentPage", 0);        // 重新设置为0
                $api.html(content, tempFn(ret['list']));
                
            } else {
                $api.append(content, tempFn(ret['list']));
            }


        } else {
           /* api.alert({
                msg: ('错误码：' + err.code + '；错误信息：' + err.msg + '网络状态码：' + err.statusCode)
            });*/

        }
        ;
    });
    

}


function getBanner() {
    
    api.ajax({
        url: 'http://wap.jqmao.cn/app',
        method: 'get',
        dataType: 'json'
    }, function (ret, err) {

        if (ret) {
            var content = $api.byId('banner-content');
            //alert(JSON.stringify(ret))
            //$api.remove('content');
            //var tpl = $api.byId('banner-template').text;
            //var tempFn = doT.template(tpl);
            //$api.html(content,tempFn(ret))

            initSlide(ret);
            
        } else {
            ////alert(JSON.stringify(err))
        }
    });

//  ajaxRequest(getTabBarBannerUrl + , 'GET', '', function (ret, err) {
//      if (ret) {
//          var content = $api.byId('banner-content');
//          var tpl = $api.byId('banner-template').text;
//          var tempFn = doT.template(tpl);
//          content.innerHTML = tempFn(ret[0].banner);
//          
//          initSlide();
//      } else {
//          api.alert({
//              msg: err.msg
//          });
//      }
//  })
}

//toggle GOODS NAV
function showNav(type) {
    if (!type) {
        return;
    }
    var nav = $api.dom('#g-nav .active');

    $api.removeCls(nav, 'active');
    var thisNav = $api.dom('#g-nav .' + type);
    $api.addCls(thisNav, 'active');
}
//获取是哪个事件
function getevt() {
    var ul = $api.byId('g-ul');
    $api.addEvt(ul, 'click', function (e) {
        var dataName = e.target.getAttribute("data-name");
        $api.rmStorage('type');
        $api.setStorage('type',dataName);
        //alert($api.getStorage('type'))
        if (dataName && e.target.tagName.toLowerCase() == 'li') {    
        
            switch (dataName) {
                case 'hot':
                    showNav(dataName);
                    getData(dataName,true);
                   
                    break;
                case 'new':
                    showNav(dataName);
                    getData(dataName,true);
                    
                    break;
                 case 'tempo':
                    showNav(dataName);
                    getData(dataName,true);
                    
                    break;   
                case 'num':
                    showNav(dataName);
                   getData(dataName,true);
                   
                    break;
            }
        }
    });
}

    //Winning information display
    function getWinInfo(){
        api.ajax({
        url: 'http://wap.jqmao.cn/getWinList',
        method: 'get',
        dataType: 'json'
    }, function (ret, err) {

        if (ret) {
             var content = $api.byId('ging-ul');
            //$api.remove('content');
            var tpl = $api.byId('message-template').text;
            var tempFn = doT.template(tpl);
            $api.html(content,tempFn(ret))
        } else {
            ////alert(JSON.stringify(err))
        }
    });
  }
    

  
   
   var ul = $("#ging-ul");
   var intervalLength = 0;
     var goInterval = function () {
        var li = $("#ging-ul li").last();  //ĩβ
        var marginTop = 40;
        ul.prepend(li).css("marginTop", "-" + marginTop + "px");
        ul.animate({marginTop: "0px"}, 200);
    }
  
   
    getWinInfo();
    var interval = setInterval(goInterval, 5000);
    getBanner();
    getData("hot",true);
    $api.rmStorage('type');
    $api.setStorage('type',"hot");
    getevt()
    $api.css($api.byId('wrap'),'display:block');


    //下拉刷新
  /* api.setRefreshHeaderInfo({
        visible: true,
        //loadingImgae: 'wgt://image/refresh-white.png',
        bgColor: '#f2f2f2',
        textColor: '#4d4d4d',
        textDown: '下拉刷新...',
        textUp: '松开刷新...',
        showTime: true
    }, function (ret, err) {
        var typeName = $api.getStorage('type');
        getBanner();
        getWinInfo();
        //alert(typeName)
        getData(typeName,true);
        $api.rmStorage('type');
        $api.setStorage('type',typeName);
        showNav(typeName);
        api.refreshHeaderLoadDone();
    });*/
    api.setCustomRefreshHeaderInfo({
    bgColor: '#fff',
    isScale: true,
    image: {
        pull: 'widget://image/refresh/1.png', 
        transform: [
            'widget://image/refresh/1.png', 
            'widget://image/refresh/2.png',
            'widget://image/refresh/3.png',
            'widget://image/refresh/4.png',
            'widget://image/refresh/5.png',
            'widget://image/refresh/6.png',
            'widget://image/refresh/7.png'
        ],
        load: [ 
            'widget://image/refresh/1.png', 
            'widget://image/refresh/2.png',
            'widget://image/refresh/3.png', 
            'widget://image/refresh/4.png',
            'widget://image/refresh/5.png'
        ]
    }
}, function() {

  			var typeName = $api.getStorage('type');   
  	 
	        //alert(typeName)	      
            //下拉刷新被触发，自动进入加载状态，使用 api.refreshHeaderLoadDone() 手动结束加载中状态
            getWinInfo();      
			getData(typeName,true);
	        $api.rmStorage('type');
	        $api.setStorage('type',typeName);
	        showNav(typeName);
          	setTimeout(function(){  
            api.refreshHeaderLoadDone();
            return;               
	    }, 800); 
	    

  
  
        
           
      });


//上拉加载
    api.addEventListener({

        name: 'scrolltobottom',
        extra: {
            threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
        }
    }, function (ret, err) {
        $("#more").show();
        var _typeName = $api.getStorage('type');
        var _page=parseInt($api.getStorage("currentPage"));
        if(_page+10<=total){

         getData(_typeName,false,_page+10);
        $api.setStorage("currentPage", _page+10); // 页码+1
        }else{
            $("#more").hide();
        }
      
       
    });
    
   /* api.setCustomRefreshHeaderInfo({
    bgColor: '#C0C0C0',
    image: {
        pull: 'widget://image/refresh/pulling.png', 
        transform: [
            'widget://image/refresh/transform0.png', 
            'widget://image/refresh/transform1.png',
            'widget://image/refresh/transform2.png',
            'widget://image/refresh/transform3.png',
            'widget://image/refresh/transform4.png',
            'widget://image/refresh/transform5.png',
            'widget://image/refresh/transform6.png'
        ],
        load: [ 
            'widget://image/refresh/loading0.png', 
            'widget://image/refresh/loading1.png',
            'widget://image/refresh/loading2.png', 
            'widget://image/refresh/loading3.png',
            'widget://image/refresh/loading4.png'
        ]
    }
}, function() {
    //下拉刷新被触发，自动进入加载状态，使用 api.refreshHeaderLoadDone() 手动结束加载中状态
    //下拉刷新被触发，使用 api.refreshHeaderLoadDone() 结束加载中状态  
    alert('开始加载刷新数据，摇一摇停止加载状态');
});*/
           /* api.addEventListener({
               name:'shake'
             },function(ret,err){
               api.refreshHeaderLoadDone()
            });
*/

};
